//  Date:         	08/08/2018
//  task:         	water analysis
//  project:      	World Development

clear all
set more off
version 13.1
graph drop _all
capture log close
set scheme plotplainblind, permanently

capture do make_index_gr.do
log using Analysis_water_endline.log,replace text

//  #1
//  load data
use water_admin_final.dta, clear
******************************
* Control
******************************
	center adult_pop HHI_Ethnicity literacy_lc1 employed_share_lc1 secondary_lc1 Nonagriculture_share_lc1 poverty_census_sc, standardize

	gl controls c_adult_pop c_HHI_Ethnicity c_literacy c_employed_share c_secondary c_Nonagriculture_share c_poverty_census_sc
	su $controls

************************************
* First round of water admin data
************************************

* assign value zero to those not mentioned in the admin data 
*standardize variables 
foreach var in parts_services_14_16 parts_services_13_14 village_requests_14_16 village_requests_13_14{
	quietly summarize `var' if TREATMENT_FINAL==0
	local `var'_mean= r(mean)
	local `var'_sd= r(sd) 
	gen c_`var' = (`var'-``var'_mean')/``var'_sd'
	qui egen mean_std_`var'=mean(c_`var') if TREATMENT_FINAL==1
	replace c_`var' = mean_std_`var' if TREATMENT_FINAL==1 & c_`var'==.
	replace c_`var' = 0 if TREATMENT_FINAL==0 & c_`var'==.
	}
	
* ttest	
foreach y in parts_services_14_16 village_requests_14_16 c_parts_services_14_16 c_village_requests_14_16 {	
	ttest `y', by(TREATMENT_FINAL) uneq
	}

* 1. No controls (except for baseline)
* 2. Add controls 
set more off
foreach y in parts_services village_requests c_parts_services c_village_requests{
	reg `y'_14_16 TREATMENT_FINAL `y'_13_14, cl(cluster_id)
		estadd local control "no"
		estadd local period "post"
		est store `y'1
		margins, at(TREATMENT_FINAL=(0 1))
		test _b[TREATMENT_FINAL]=0
		local sign_trt = sign(_b[TREATMENT_FINAL])
		display "Ho: coef <= 0  p-value = " ttail(r(df_r),`sign_trt'*sqrt(r(F)))
		
	reg `y'_14_16 TREATMENT_FINAL `y'_13_14 $controls, cl(cluster_id)
		estadd local control "yes"
		estadd local period "post"
		est store `y'2
		margins, at(TREATMENT_FINAL=(0 1))
		test _b[TREATMENT_FINAL]=0
		local sign_trt = sign(_b[TREATMENT_FINAL])
		display "Ho: coef <= 0  p-value = " ttail(r(df_r),`sign_trt'*sqrt(r(F))) 
}

************************************
* Second round of water admin data
************************************
*standarize variables 
foreach var in A2_LC1Request0 A2_LC1Request1 A2_LC1Request2{
	replace  `var'=0 if  `var'==. & TREATMENT_FINAL!=.
	quietly summarize `var' if TREATMENT_FINAL==0
	local mean`var'= r(mean)
	local sd`var'= r(sd) 
	gen c_`var' = (`var'-`mean`var'')/`sd`var''
	}

foreach y in A2_LC1Request1 A2_LC1Request2 c_A2_LC1Request1 c_A2_LC1Request2{	
	ttest `y', by(TREATMENT_FINAL) uneq
	}
	
	*Regressions:
		*SHORT-TERM  
		reg c_A2_LC1Request1 TREATMENT_FINAL c_A2_LC1Request0, cl(cluster_id)
		estadd local control "no"
		estadd local period "short-term"
		est store c_A2_LC1Request1a
		margins, at(TREATMENT_FINAL=(0 1))
		test _b[TREATMENT_FINAL]=0
		local sign_trt = sign(_b[TREATMENT_FINAL])
		display "Ho: coef <= 0  p-value = " ttail(r(df_r),`sign_trt'*sqrt(r(F)))
		
		reg c_A2_LC1Request1 TREATMENT_FINAL c_A2_LC1Request0 $controls, cl(cluster_id)
		estadd local control "yes"
		estadd local period "short-term"
		est store c_A2_LC1Request1b
		margins, at(TREATMENT_FINAL=(0 1))	
		test _b[TREATMENT_FINAL]=0
		local sign_trt = sign(_b[TREATMENT_FINAL])
		display "Ho: coef <= 0  p-value = " ttail(r(df_r),`sign_trt'*sqrt(r(F)))
			
		* LONG-TERM (standardized)
		reg c_A2_LC1Request2 TREATMENT_FINAL c_A2_LC1Request0, cl(cluster_id)
		estadd local control "no"
		estadd local period "long-term"
		est store c_A2_LC1Request2a
		margins, at(TREATMENT_FINAL=(0 1))
		test _b[TREATMENT_FINAL]=0
		local sign_trt = sign(_b[TREATMENT_FINAL])
		display "Ho: coef <= 0  p-value = " ttail(r(df_r),`sign_trt'*sqrt(r(F)))
		
		reg c_A2_LC1Request2 TREATMENT_FINAL c_A2_LC1Request0 $controls, cl(cluster_id)
		estadd local control "yes"
		estadd local period "long-term"
		est store c_A2_LC1Request2b
		margins, at(TREATMENT_FINAL=(0 1))	
		test _b[TREATMENT_FINAL]=0
		local sign_trt = sign(_b[TREATMENT_FINAL])
		display "Ho: coef <= 0  p-value = " ttail(r(df_r),`sign_trt'*sqrt(r(F)))		

***************************************************
* TABLE 3 in Supplementary Information
***************************************************

*relabeling to 
*producing summary statistics 
lab var parts_services_14_16 "Parts and services post-treatment"
lab var parts_services_13_14 "Parts and services baseline" 
lab var village_requests_14_16 "Village requests post-treatment" 
lab var village_requests_13_14 "Village requests baseline" 
lab var A2_LC1Request0 "Village requests (2nd round data) baseline"
lab var A2_LC1Request1  "Village requests (2nd round data) midline"
lab var A2_LC1Request2  "Village requests (2nd round data) endline"

summ parts_services_14_16 parts_services_13_14 village_requests_14_16 village_requests_13_14 A2_LC1Request0 A2_LC1Request1 A2_LC1Request2

log close 
clear
exit